<template>
  <div class="search">
    <van-nav-bar
      :title="keyword + '的搜索结果'"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- 搜索结果 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id"
        :title="item.title"
        @click="$router.push('/article/' + item.art_id)"
      />
    </van-list>
  </div>
</template>

<script>
import { searchResultFn } from '@/api/search'
export default {
  name: 'SearchResult',
  data() {
    return {
      loading: false,
      finished: false,
      page: 1,
      list: []
    }
  },
  methods: {
    async onLoad() {
      console.log('正在加载中')
      const res = await searchResultFn(this.page, this.keyword)
      console.log(res)
      this.list = [...this.list, ...res.data.results]
      this.loading = false
      if (res.data.results.length === 0) {
        this.finished = true
      }
      this.page++
    }
  },
  computed: {
    keyword() {
      return this.$route.query.keyword
    }
  }
}
</script>

<style></style>
